import axios from 'axios';
import type { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import type { floor, homefloor, sublib } from '@/type';
declare module 'axios' {
  interface AxiosResponse<T = any> {
    vr_ifa_Lib_GetFloorMap_list4: floor[];
    vr_ifa_Lib_GetFloor_list1: sublib[];
    vr_ifa_Lib_GetFloor_list2: homefloor[];
    // 这里追加你的参数
  }
  export function create(config?: AxiosRequestConfig): AxiosInstance;
}

//创建对象实例
const serivce: AxiosInstance = axios.create({
  baseURL: import.meta.env.VITE_API_IFQ as string,
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
});
//请求拦截器

serivce.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  if (config.headers.base64) {
    config.params = {
      ...config.params,
      x: '',
      y: '01',
    };
  }
  return config;
});
//响应拦截器
serivce.interceptors.response.use(
  (response: AxiosResponse) => {
    return response.data;
  },
  (error: any) => {
    let message = '';
    let status = error.response.status;
    switch (status) {
      case 401:
        message = 'TOKEN过期了';
        break;
      case 403:
        message = '无权访问';
        break;
      case 404:
        message = '请求地址错误';
        break;
      case 500:
        message = '服务器出现问题';
        break;
      default:
        message = '网络出现问题';
        break;
    }
    ElMessage({
      type: 'error',
      message,
    });
    return Promise.reject(error);
  }
);

export default serivce;
